import 'package:flutter/material.dart';

class RotationImage extends StatefulWidget {
  final Widget image;
  // 旋转一圈的时间
  final Duration duration;
  final double? width;
  final double? height;
  final bool repeat;

  const RotationImage(
      {Key? key,
      required this.image,
      this.width,
      this.height,
      this.duration = const Duration(seconds: 1),
      this.repeat = true})
      : super(key: key);
  @override
  _RotationImageState createState() => _RotationImageState();
}

class _RotationImageState extends State<RotationImage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    _controller = AnimationController(
      vsync: this,
      duration: widget.duration,
    )..repeat(reverse: false);

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.width,
      height: widget.height,
      child: RotationTransition(
        //设置动画的旋转中心
        alignment: Alignment.center,
        //动画控制器
        turns: _controller,
        //将要执行动画的子view
        child: widget.image,
      ),
    );
  }

  @override
  void dispose() {
    _controller.stop();
    _controller.dispose();
    super.dispose();
  }
}
